Ext.namespace("admin.systemManage");

admin.systemManage.module = function(pannelId, title, iconClz){

	var allModule = [];
	loadModuleTree();
	var troot = new Ext.tree.TreeNode({
		text : "root"
	});

	var parentModuleId = null;
	var moduleTreePanel = new Ext.tree.TreePanel({
		title : "系统模块树",
		flex : 2,
		frame : true,
		root : troot,
		autoScroll : true,
		rootVisible : false,
		listeners : {
			click : function(node){
				parentModuleId = node.attributes.mId;
				childModuleStore.reload();
			}
		}
	});

	var optBar = [{
		text : "创建",
		iconCls : "create",
		handler : function(){
			addModule("admin/module/add");
		}
	}, "-", {
		text : "删除",
		iconCls : "delete",
		handler : function(){
			var rec = sm.getSelected();
			if(rec===undefined){
				showFailMsg("请选择子模块.");
				return;
			}
			confirmDelete(del);
			function del(){
				ajaxRequest({
					url : "admin/module/delete/" + rec.get("id"),
					showMsg : "删除成功.",
					callback : function(){
						childModuleStore.reload();
					}
				});
			}
		}
	}, "-", {
		text : "更新",
		iconCls : "update",
		handler : function(){
			updateModule("admin/module/update");
		}
	}, "-", {
		text : "切换状态",
		iconCls : "toggleStatus",
		handler : function(){
			var rec = sm.getSelected();
			if(rec===undefined){
				showFailMsg("请选择模块.");
				return;
			}
			ajaxRequest({
				url : "admin/module/toggleStatus/" + rec.get("id"),
				showMsg : "切换状态成功.",
				callback : function(){
					childModuleStore.reload();
				}
			});
		}
	}];

	var childModuleStore = new Ext.data.JsonStore({
		url : "admin/module/listModuleByParent",
		totalProperty : "total",
		root : "rows",
		fields : ["id", "createTime", "updateTime", "chinese", "english",
			"url", "iconClz", "orderNum", "status"],
		listeners : {
			beforeload : function(store){
				store.removeAll();
				Ext.apply(store.baseParams, {
					id : parentModuleId
				});
			}
		}
	});

	var cm = new Ext.grid.ColumnModel([{
		header : "模块名称",
		dataIndex : "chinese"
	}, {
		header : "模块英文名称",
		dataIndex : "english",
		width : 200
	}, {
		header : "图标",
		dataIndex : "iconClz"
	}, {
		header : "顺序",
		dataIndex : "orderNum"
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderStatus
	}]);
	var sm = new Ext.grid.RowSelectionModel({
		singleSelect : true
	});

	var childGrid = new Ext.grid.GridPanel({
		title : "子模块列表",
		frame : true,
		flex : 8,
		store : childModuleStore,
		cm : cm,
		sm : sm,
		tbar : genToolBar(optBar, [], pannelId)
	});

	return new Ext.Panel({
		title : title,
		id : pannelId,
		iconCls : iconClz,
		closable : true,
		layout : "hbox",
		layoutConfig : {
			align : "stretch"
		},
		items : [moduleTreePanel, childGrid]
	});

	function loadModuleTree(){
		ajaxRequest({
			url : "admin/module/allModule",
			callback : function(json){
				allModule = $.makeArray(json.data);
				initModuleTree();
			}
		});
	}

	//根据全部module生成完整的module tree
	function initModuleTree(){
		var levelOne = $.grep(allModule, function(o){
			return o.parent==null
		});
		$.each(levelOne, function(n, v){
			var mNode = new Ext.tree.TreeNode({
				mId : v.id,
				text : v.chinese,
				iconCls : v.iconClz,
				expanded : true
			});
			troot.appendChild(mNode);
			addChildren(v, mNode);

			function addChildren(p, root){
				$.each(allModule, function(n, m){
					if(m.parent!=null && m.parent.id== p.id){
						var child = new Ext.tree.TreeNode({
							mId : m.id,
							text : m.chinese,
							iconCls : m.iconClz,
							expanded : true
						});
						root.appendChild(child);
						addChildren(m, child);
					}
				});
			}
		});
		moduleTreePanel.expandAll();
		troot.cascade(function(t){
//			t.addListener("beforeclick", function(){
//				if(!t.hasChildNodes()){
//					return false;
//				}
//			});
		});
	}

	function genAddOrUpdateWindow(url){
		return new Ext.Window({
			title : "新增或修改模块",
			layout : "fit",
			modal : true,
			iconCls : "save",
			width : 320,
			height : 280,
			plain : true,
			bodyStyle : 'padding:5px;',
			items : genForm(url)
		});
		function genForm(url){
			return new Ext.form.FormPanel({
				url : url,
				labelWidth : 80,
				labelAlign : "right",
				baseCls: 'x-plain',
				defaults : {
					xtype : "textfield",
					anchor : "95%"
				},
				items : [{
					xtype : "hidden",
					name : "id",
					//value设置-1，为空则controller检验失败
					value : "-1"
				}, {
					fieldLabel : "模块中文名称",
					allowBlank : false,
					name : "chinese"
				}, {
					fieldLabel : "模块英文名称",
					allowBlank : false,
					name : "english"
				}, genStatusCombo(), {
					xtype : "numberfield",
					fieldLabel : "顺序",
					allowBlank : false,
					name : "orderNum"
				}, {
					fieldLabel : "图标",
					name : "iconClz"
				}, {
					xtype : "hidden",
					name : "parentId",
					value : moduleTreePanel.getSelectionModel().getSelectedNode().attributes.mId
				}, {
					fieldLabel : "描述",
					xtype : "textarea",
					name : "des"
				}],
				buttonAlign : "center",
				buttons : [{
					text : "保存",
					iconCls : "save",
					handler : function(b){
						submitForm(b.findParentByType("form"), refreshGrid);
						function refreshGrid(){
							childModuleStore.reload();
						}
					}
				}, {
					text : "重置",
					iconCls : "reset",
					type : "reset",
					handler : function(b){
						b.findParentByType("form").getForm().reset();
					}
				}]
			});
		}
	}

	function addModule(url){
		if(moduleTreePanel.getSelectionModel().getSelectedNode()==null){
			showFailMsg("请选择父模块.");
			return;
		}
		var win = genAddOrUpdateWindow(url);
		win.show();
	}
	function updateModule(url){
		var rec = sm.getSelected();
		if(rec==undefined){
			showFailMsg("请选择子模块.");
			return;
		}
		var win = genAddOrUpdateWindow(url);
		win.findByType("form")[0].getForm().loadRecord(rec);
		win.show();
	}
}

registerModule("admin.systemManage.module", admin.systemManage.module);